﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using DevExpress.XtraTreeMap;
using Lamex.mMES.Common;

namespace Lamex.mMES.UI
{
    public partial class frmMES_Lab_TreeMap : frmBaseChild
    {
        public frmMES_Lab_TreeMap()
        {
            InitializeComponent();
        }

        //https://documentation.devexpress.com/WindowsForms/115719/Controls-and-Libraries/TreeMap-Control/Getting-Started/Lesson-1-Create-a-Simple-Unbound-TreeMap#populatedata
        private void frmMES_Work_Flow_Kanban_Load(object sender, EventArgs e)
        {
            string strSql = @"SELECT SUB_FACTORY,WORKING_HOURS*CURRENT_WORKERS AS RESOURSE_QTY
                            FROM View_OP_Resource_Plan";

            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            ConfigureTreeMapDataAdapter1();
            ConfigureTreeMapColorizer(treeMapControl1);

            ConfigureTreeMapDataAdapter2();
            ConfigureTreeMapColorizer(treeMapControl2);

            //TreeMapHierarchicalDataAdapter adapter2 = new TreeMapHierarchicalDataAdapter
            //{
            //    DataSource = dt,
            //};

            //treeMapControl2.DataAdapter = adapter2;
        }

        #region #FlatDataAdapter
        private void ConfigureTreeMapDataAdapter1()
        {
            TreeMapFlatDataAdapter adapter1 = new TreeMapFlatDataAdapter
            {
                DataSource = CreateBillionaireInfos1(),
                LabelDataMember = "Name",
                ValueDataMember = "NetWorth"
            };
            adapter1.GroupDataMembers.Add("Residence");
            treeMapControl1.DataAdapter = adapter1;
        }

        private void ConfigureTreeMapDataAdapter2()
        {
            TreeMapFlatDataAdapter adapter2 = new TreeMapFlatDataAdapter
            {
                DataSource = CreateBillionaireInfos2(),
                LabelDataMember = "WorkCemter",
                ValueDataMember = "Quota"
            };
            adapter2.GroupDataMembers.Add("SubFactory");
            treeMapControl2.DataAdapter = adapter2;
        }

        #endregion #FlatDataAdapter

        #region #GroupGradientColorizer
        private void ConfigureTreeMapColorizer(TreeMapControl treeMapControl)
        {
            treeMapControl.Colorizer = new TreeMapGroupGradientColorizer
            {
                GradientColor = Color.Transparent,
                Max = 1,
                Min = 0.3,
                Palette = Palette.Office2013Palette,
            };
        }
        #endregion #GroupGradientColorizer

        List<BillionaireInfo1> CreateBillionaireInfos1()
        {
            List<BillionaireInfo1> infos1 = new List<BillionaireInfo1> {
                new BillionaireInfo1 {Name = "Bill Gates",           NetWorth = 79.2,    Age = 60,   Residence = "United States",  Source = "Microsoft"},
                new BillionaireInfo1 {Name = "Carlos Slim Helu",     NetWorth = 77.1,    Age = 75,   Residence = "Mexico",         Source = "telecom"},
                new BillionaireInfo1 {Name = "Warren Buffett",       NetWorth = 72.7,    Age = 85,   Residence = "United States",  Source = "Berkshire Hathaway"},
                new BillionaireInfo1 {Name = "Amancio Ortega",       NetWorth = 64.5,    Age = 79,   Residence = "Spain",          Source = "Zara"},
                new BillionaireInfo1 {Name = "Larry Ellison",        NetWorth = 54.3,    Age = 71,   Residence = "United States",  Source = "Oracle"},
                new BillionaireInfo1 {Name = "Charles Koch",         NetWorth = 42.9,    Age = 79,   Residence = "United States",  Source = "diversified"},
                new BillionaireInfo1 {Name = "David Koch",           NetWorth = 42.9,    Age = 75,   Residence = "United States",  Source = "diversified"},
                new BillionaireInfo1 {Name = "Christy Walton",       NetWorth = 41.7,    Age = 60,   Residence = "United States",  Source = "Wal-Mart"},
                new BillionaireInfo1 {Name = "Jim Walton",           NetWorth = 40.6,    Age = 67,   Residence = "United States",  Source = "Wal-Mart"},
                new BillionaireInfo1 {Name = "Liliane Bettencourt",  NetWorth = 40.1,    Age = 93,   Residence = "France",         Source = "L'Oreal"}
            };
            return infos1;
        }

        List<BillionaireInfo2> CreateBillionaireInfos2()
        {
            List<BillionaireInfo2> infos2 = new List<BillionaireInfo2> {
                new BillionaireInfo2 {WorkCemter = "D1_主线",     Quota = 79,    SubFactory = "生产-桌子"},
                new BillionaireInfo2 {WorkCemter = "D2_线下",     Quota = 7,     SubFactory = "生产-桌子"},
                new BillionaireInfo2 {WorkCemter = "P1_铝材CNC",  Quota = 72,    SubFactory = "生产-屏风"},
                new BillionaireInfo2 {WorkCemter = "P2_木器CNC",  Quota = 64,    SubFactory = "生产-屏风"},
            };
            return infos2;
        }

        private void frmMES_Work_Flow_Kanban_Resize(object sender, EventArgs e)
        {
            panelLeft.Width = this.Width / 2;
            panelRight.Width = this.Width / 2;

            panelRgt_Top.Height = panelRight.Height / 2;
        }
    }
}